package com.lxw.nonghe.controller;

import com.lxw.nonghe.annotation.RequiresPermission;
import com.lxw.nonghe.common.Result;
import com.lxw.nonghe.entity.OperationLog;
import com.lxw.nonghe.service.*;
import com.lxw.nonghe.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 首页控制器
 */
@RestController
@RequestMapping("/home")
@RequiresPermission("home:manage")
public class HomeController {

    @Autowired
    private UserService userService;

    @Autowired
    private ChronicDiseaseCertificateService chronicDiseaseCertificateService;

    @Autowired
    private ChronicDiseaseReimbursementService chronicDiseaseReimbursementService;

    @Autowired
    private MedicalInstitutionService medicalInstitutionService;

    @Autowired
    private LogUtils logUtils;
    @Autowired
    private OperationLogService operationLogService;

    @GetMapping("/userCount")
    public Result<Long> getUserCount() {
        try {
            long count = userService.getUserCount();
            /*logUtils.recordLog("首页数据", "获取用户数量", "成功", "总数:" + count);*/
            return Result.success("获取用户数量成功", count);
        } catch (Exception e) {
            logUtils.recordLog("首页数据", "获取用户数量", "失败", e.getMessage());
            return Result.error("获取用户数量失败：" + e.getMessage());
        }
    }

    @GetMapping("/policyCount")
    public Result<Long> getPolicyCount() {
        try {
            long count = chronicDiseaseCertificateService.getPolicyCount();
            /*logUtils.recordLog("首页数据", "获取慢病档案数量", "成功", "总数:" + count);*/
            return Result.success("获取慢病档案数量成功", count);
        } catch (Exception e) {
            logUtils.recordLog("首页数据", "获取慢病档案数量", "失败", e.getMessage());
            return Result.error("获取慢病档案数量失败：" + e.getMessage());
        }
    }

    @GetMapping("/medicalInstitutionCount")
    public Result<Long> getMedicalInstitutionCount() {
        try {
            long count = medicalInstitutionService.getMedicalInstitutionCount();
            /*logUtils.recordLog("首页数据", "获取医疗机构数量", "成功", "总数:" + count);*/
            return Result.success("获取医疗机构数量成功", count);
        } catch (Exception e) {
            logUtils.recordLog("首页数据", "获取医疗机构数量", "失败", e.getMessage());
            return Result.error("获取医疗机构数量失败：" + e.getMessage());
        }
    }

    @GetMapping("/totalReimbursementAmount")
    public Result<BigDecimal> getTotalReimbursementAmount() {
        try {
            BigDecimal totalAmount = chronicDiseaseReimbursementService.getTotalReimbursementAmount();
            /*logUtils.recordLog("首页数据", "获取报销金额合计", "成功", "金额:" + totalAmount);*/
            return Result.success("获取报销金额合计成功", totalAmount);
        } catch (Exception e) {
            logUtils.recordLog("首页数据", "获取报销金额合计", "失败", e.getMessage());
            return Result.error("获取报销金额合计失败：" + e.getMessage());
        }
    }

    @GetMapping("/getRegisterTimes")
    public Result<int[]> getRegisterTimes() {
        try {
            int[] registerTimes = userService.getRegisterTimes();
            /*logUtils.recordLog("首页数据", "获取每月注册用户数", "成功",
                    "数据量:" + registerTimes.length);*/
            return Result.success("每月注册用户数量查询成功", registerTimes);
        } catch (Exception e) {
            logUtils.recordLog("首页数据", "获取每月注册用户数", "失败", e.getMessage());
            return Result.error("获取每月注册用户数失败：" + e.getMessage());
        }
    }

    @GetMapping("/getMonthlyCount")
    public Result<int[]> getMonthlyReimbursementCount() {
        try {
            int[] counts = chronicDiseaseReimbursementService.getReimbursementCountByMonth();
            /*logUtils.recordLog("首页数据", "获取每月报销数量", "成功",
                    "数据量:" + counts.length);*/
            return Result.success("每月报销数量查询成功", counts);
        } catch (Exception e) {
            logUtils.recordLog("首页数据", "获取每月报销数量", "失败", e.getMessage());
            return Result.error("获取每月报销数量失败：" + e.getMessage());
        }
    }
    @GetMapping("/recentLogs")
    public Result<List<OperationLog>> getRecentLogs() {
        try {
            List<OperationLog> logs = operationLogService.getRecentLogs();
            List<OperationLog> formattedLogs = new ArrayList<>();
            for (OperationLog log : logs) {
                OperationLog formattedLog = new OperationLog();
                formattedLog.setLogId(log.getLogId());
                formattedLog.setUserId(log.getUserId());
                formattedLog.setUsername(log.getUsername());
                formattedLog.setModule(log.getModule());
                formattedLog.setOperation(log.getOperation());
                formattedLog.setStatus(log.getStatus());
                formattedLog.setErrorMsg(log.getErrorMsg());
                formattedLog.setOperationTime(log.getOperationTime());
                formattedLog.setFormattedOperationTime(log.getFormattedOperationTime());
                formattedLogs.add(formattedLog);
            }
            /*logUtils.recordLog("日志管理", "获取最近操作日志", "成功",
                    "获取数量:" + (formattedLogs != null ? formattedLogs.size() : 0));
            System.out.println("formattedLogs:"+formattedLogs);*/
            return Result.success("获取最近操作日志成功", formattedLogs);
        } catch (Exception e) {
            logUtils.recordLog("日志管理", "获取最近操作日志", "失败", e.getMessage());
            return Result.error("获取最近操作日志失败：" + e.getMessage());
        }
    }
}
